package org.example.filter;

import java.io.IOException;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class TenantFilter extends OncePerRequestFilter {

    /**
     * 示例代码做了以下工作：
     * 从请求头中获取租户ID。
     * 检查当前用户是否对租户ID有访问权。
     * 如果该用户有访问权，那么就调用链中其他的 filter。
     * 如果用户没有权限，则抛出一个 AccessDeniedException。
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String tenantId = request.getHeader("X-Tenant-Id");
        boolean hasAccess = isUserAllowed(tenantId);
        if (hasAccess) {
            filterChain.doFilter(request, response);
            return;
        }
        throw new AccessDeniedException("Access denied");
    }
    public boolean isUserAllowed(String tenantId){
        return true;
    }

}